import requests
import json
from ..shippypro import api_key, headers, shippypro_url
from .base_api import BaseAPI

key = api_key
headers = headers
url = shippypro_url


class ShipmentAPI(BaseAPI):

    @classmethod
    def getRates(
        cls,
        receive_name: str,
        receive_company_name: str,
        receive_phone: str,
        receive_email: str,
        receive_country: str,
        receive_state: str,
        receive_city: str,
        receive_street: str,
        receive_postal_code: str,
        send_name: str,
        send_company_name: str,
        send_phone: str,
        send_email: str,
        send_country: str,
        send_state: str,
        send_city: str,
        send_street: str,
        send_postal_code: str,
        parcels_list: list,
        parcel_value: float,
        parcel_desc: str,
        insurance_price: float,
        insurance_currency: str,
        # sss,
    ) -> dict:
        return super().post(
            "GetRates",
            {
                "to_address": {
                    "name": receive_name,
                    "company": receive_company_name,
                    "street1": receive_street,
                    "street2": "",
                    "city": receive_city,
                    "state": receive_state,
                    "zip": receive_postal_code,
                    "country": receive_country,
                    "phone": receive_phone,
                    "email": receive_email,
                },
                "from_address": {
                    "name": send_name,
                    "company": send_company_name,
                    "street1": send_street,
                    "street2": "",
                    "city": send_city,
                    "state": send_state,
                    "zip": send_postal_code,
                    "country": send_country,
                    "phone": send_phone,
                    "email": send_email,
                },
                "parcels": parcels_list,
                "Insurance": insurance_price,
                "InsuranceCurrency": insurance_currency,
                "CashOnDelivery": 0,  # 货到付款
                "CashOnDeliveryCurrency": "EUR",  # 货到付款单位
                "ContentDescription": parcel_desc,
                "TotalValue": parcel_value,
                "ShippingService": "Standard",
            },
        )

    @classmethod
    def ship(
        cls,
        receive_name: str,
        receive_company_name: str,
        receive_phone: str,
        receive_email: str,
        receive_country: str,
        receive_state: str,
        receive_city: str,
        receive_street: str,
        receive_postal_code: str,
        send_name: str,
        send_company_name: str,
        send_phone: str,
        send_email: str,
        send_country: str,
        send_state: str,
        send_city: str,
        send_street: str,
        send_postal_code: str,
        parcels_list: list,
        parcel_value: float,
        parcel_desc: str,
        insurance_price: float,
        insurance_currency: str,
        courier_name: str,
        courier_service_name: str,
        courier_service_code: int,
        order_code: str,
        transaction_code: str,
        payment_method: str,
        courier_options: list,
    ) -> dict:
        """
        创建运单
        :param receive_name: 收件人名称
        :param receive_company_name: 收件人公司名称
        :param receive_phone: 收件人电话
        :param receive_email: 收件人邮箱
        :param receive_country: 收件人国家代码
        :param receive_state: 收件人省份
        :param receive_city: 收件人城市
        :param receive_street: 收件人街道
        :param receive_postal_code: 收件人邮编

        :param send_name: 发件人名称
        :param send_company_name: 发件人公司名称
        :param send_phone: 发件人电话
        :param send_email: 发件人邮箱
        :param send_country: 发件人国家代码
        :param send_state: 发件人省份
        :param send_city: 发件人城市
        :param send_street: 发件人街道
        :param send_postal_code: 发件人邮编

        :param parcels_list: 包裹集合[{"length": 5,"width": 5,"height": 5,"weight": 10,"Attributes": [{"name": "parcelID",value": "123456"},
                                                                                                    {"name": "parcelType","value": "Box"}]}]
        :param parcel_value: 包裹价值,带货币单位缩写"123 EUR"
        :param parcel_desc: 包裹描述

        :param insurance_price: 保险价格
        :param insurance_currency: 保险货币单位
        :param courier_name: 承运商名称
        :param courier_service_name: 承运商服务名称
        :param courier_service_code: ShipPyPro中承运商服务编码
        :param order_code: 商业订单编码
        :param transaction_code: 交易订单编码
        :param payment_method: 支付方式
        :param courier_options: 承运商增值[{"name": " ","value": "0"}]
        """
        return super().post(
            "Ship",
            {
                "to_address": {
                    "name": receive_name,
                    "company": receive_company_name,
                    "street1": receive_street,
                    "street2": "",
                    "city": receive_city,
                    "state": receive_state,
                    "zip": receive_postal_code,
                    "country": receive_country,
                    "phone": receive_phone,
                    "email": receive_email,
                },
                "from_address": {
                    "name": send_name,
                    "company": send_company_name,
                    "street1": send_street,
                    "street2": "",
                    "city": send_city,
                    "state": send_state,
                    "zip": send_postal_code,
                    "country": send_country,
                    "phone": send_phone,
                    "email": send_email,
                },
                "parcels": parcels_list,
                "TotalValue": parcel_value,
                "TransactionID": transaction_code,
                "MarketplaceID": 0,  # 网店平台订单号
                "MarketplaceName": "",  # 网店平台
                "ContentDescription": parcel_desc,
                "Insurance": insurance_price,
                "InsuranceCurrency": insurance_currency,
                "CashOnDelivery": 0,  # 货到付款
                "CashOnDeliveryCurrency": "EUR",  # 货到付款单位
                "CashOnDeliveryType": 0,  # 货到付款类型
                "CarrierName": courier_name,
                "CarrierService": courier_service_name,
                "CarrierID": courier_service_code,
                "CarrierOptions": courier_options,
                "OrderID": order_code,
                "RateID": "",  # 费率编号
                "Incoterm": "DAP",  # 装货国际贸易术语
                "BillAccountNumber": "",
                "PaymentMethod": payment_method,
                "Note": "",
                "IsReturn": False,
                "Async": False,
                "CustomInfoContentsType": "",  # 装运内容类型描述
                # "CN22Info": [  # 海关
                #     {
                #         "Description": "",
                #         "Weight": 0,
                #         "Quantity": 0,
                #         "UnitValue": 0,
                #         "OriginCountry": "",
                #         "Currency": "",
                #         "HSCode": ""
                #     }
                # ],
                # "Attributes": {  # 其他参数
                #     "sp_ioss": ""
                # }
            },
        )
        # values = {
        #     "Method": "Ship",
        #     "Params":
        # }
        # data = json.dumps(values)
        # print(data)
        # response = requests.post(url, data=data, headers=headers)
        # print(response.text)
        # response_dict = json.loads(response.text)
        # return response_dict
